/*
 * Copyright (c) 2011, Andrea Palloni
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * - Neither the name of the copyright holder nor the names of its contributors may be used
 * 	  to endorse or promote products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package net.palla.biblio.server.service;

import java.util.ArrayList;
import java.util.List;

import net.palla.biblio.server.dao.PublisherDao;
import net.palla.biblio.server.entity.PublisherEntity;
import net.palla.biblio.shared.Publisher;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

/**
 * Represents a service for managing Publisher entities
 * 
 * @author andrea.palloni
 * 
 */
public class PublisherService {

	private PublisherDao dao = new PublisherDao();

	/**
	 * Converts a PublisherEntity to Publisher
	 * @param publisher
	 * @return
	 */
	private Publisher convertFromEntity(PublisherEntity publisher) {
		Publisher pub = new Publisher();
		pub.setId(KeyFactory.keyToString(publisher.getId()));
		pub.setName(publisher.getName());
		return pub;
	}

	/**
	 * Converts a Publisher to PublisherEntity
	 * 
	 * @param publisher
	 * @return
	 */
	private PublisherEntity convertToEntity(Publisher publisher) {
		PublisherEntity pub = new PublisherEntity();
		if (publisher.getId() != null) {
			Key key = KeyFactory.stringToKey(publisher.getId());
			pub.setId(key);
		}
		pub.setName(publisher.getName());
		return pub;
	}

	/**
	 * Gets a publisher by key
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public Publisher getById(String id) throws Exception {
		Key key = KeyFactory.stringToKey(id);
		PublisherEntity auth = (PublisherEntity) dao.getById(key);
		return convertFromEntity(auth);
	}

	/**
	 * Retrieves a list of publishers to be passed to the Front end.
	 * 
	 * @param name
	 * @return
	 * @throws Exception
	 */
	public List<Publisher> getByName(String name) throws Exception {
		List<PublisherEntity> publishers = dao.getByName(name);
		List<Publisher> ret = new ArrayList<Publisher>();
		for (PublisherEntity publisher : publishers) {
			ret.add(convertFromEntity(publisher));
		}
		return ret;
	}

	/**
	 * Saves or updates an entity
	 * @param publisher
	 * @return
	 * @throws Exception
	 */
	public Publisher saveOrUpdate(Publisher publisher) throws Exception {
		PublisherEntity pub = convertToEntity(publisher);
		pub = (PublisherEntity) dao.saveOrUpdate(pub);
		return convertFromEntity(pub);
	}
	
	/**
	 * Deletes an entity
	 * @param publisher
	 * @throws Exception
	 */
	public void delete(Publisher publisher) throws Exception {
		Key key = KeyFactory.stringToKey(publisher.getId());
		PublisherEntity pub = (PublisherEntity)dao.getById(key);
		dao.delete(pub);
	}
	
}
